﻿#region 命名空间

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using WinnerCMS.BLL;
using WinnerCMS.Model;
using WinnerCMS.Common;
using WinnerCMS.Model.Attribute;

#endregion

namespace WinnerCMS.WebUI.Admin.Field
{
    [Access("257005")]
    public partial class Add : WebFactory.PageBase
    {
        private byte _iModelId;
        private readonly FieldManage _myBLL = new FieldManage();

        protected void Page_Load(object sender, EventArgs e)
        {
            this._iModelId = byte.Parse(Request["ModelId"]);

            if (this._iModelId == 5)
            {
                ErrClass.Redirect("问答系统不支持自定义字段。");
            }

            if (!IsPostBack)
            {
                if (Request["ID"] != null)
                {
                    //“修改”界面
                    this.btnOK.Text = "修改";
                    Edit_Load();
                    var strPath = string.Format("{0} → 字段管理 → 修改字段", ModelManage.GetModelName(this._iModelId));
                    this.lblPath.Text = strPath;
                }
                else
                {
                    //“添加”界面
                    this.btnOK.Text = "添加";
                    var strPath = string.Format("{0} → 字段管理 → 添加字段", ModelManage.GetModelName(this._iModelId));
                    this.lblPath.Text = strPath;
                }
            }
        }

        #region 添加或修改

        private void AddUI()
        {
            WinnerCMS.Model.Field model;
            if (Request["ID"] == null)
            {
                model = new WinnerCMS.Model.Field();
                model.Id = 0;
            }
            else
            {
                model = this._myBLL.GetInfo(int.Parse(Request["ID"]));
            }
            model.ModelId = this._iModelId;
            model.FieldName = this.txtFieldName.Text.Trim();
            model.Title = this.txtTitle.Text.Trim();
            model.Tips = this.txtTips.Text.Trim();
            model.FieldType = this.ddlFieldType.SelectedValue;
            model.DefaultValue = this.txtDefaultValue.Text.Trim();
            model.ControlType = this.ddlControlType.SelectedValue;
            model.IsShowInMember = bool.Parse(this.RBL_IsShowInMember.SelectedValue);
            model.Indispensable = int.Parse(this.RBL_Indispensable.SelectedValue);
            model.Width = int.Parse(this.txtWidth.Text);
            model.MaxFieldLength = int.Parse(this.txtMaxFieldLength.Text);
            model.IsStop = bool.Parse(this.RBL_Stop.SelectedValue);


            string strErr = null;

            new FieldManage().Add(model, ref strErr);

            if (string.IsNullOrEmpty(strErr) == false)
            {
                MsgBox.Message(strErr, this.UpdatePanel1);
                return;
            }
            MsgBox.Message("操作成功！", "Manage.aspx?ModelId=" + this._iModelId, this.UpdatePanel1);
        }

        #endregion

        #region 加载

        private void Edit_Load()
        {
            var model = new FieldManage().GetInfo(int.Parse(Request["ID"]));
            if (model != null)
            {
                this.txtFieldName.Text = model.FieldName;

                this.txtTitle.Text = model.Title;
                this.txtTips.Text = model.Tips;
                this.ddlFieldType.SelectedValue = model.FieldType;
                this.txtDefaultValue.Text = model.DefaultValue;
                this.ddlControlType.SelectedValue = model.ControlType;
                this.RBL_IsShowInMember.SelectedValue = model.IsShowInMember.ToString();
                this.RBL_Indispensable.SelectedValue = model.Indispensable.ToString();
                this.txtWidth.Text = model.Width.ToString();
                this.txtMaxFieldLength.Text = model.MaxFieldLength.ToString();
                this.RBL_Stop.SelectedValue = model.IsStop.ToString();

                this.txtFieldName.Enabled = false;
                this.ddlFieldType.Enabled = false;
                this.txtMaxFieldLength.Enabled = false;
            }
            else
            {
                //转向错误页面
                ErrClass.Redirect(" 不存在。");
            }
        }

        #endregion

        protected void btnOK_Click(object sender, EventArgs e)
        {
            AddUI();
        }
    }
}